package com.cloudlinkscm.base.authcenter.service.impl;

import com.cloudlinkscm.base.authcenter.dao.PublicResourceDao;
import com.cloudlinkscm.base.authcenter.dao.TenantResourceDao;
import com.cloudlinkscm.base.authcenter.domain.AbstractPublicResourceManager;
import com.cloudlinkscm.base.authcenter.domain.AbstractTenantResourceManager;
import com.cloudlinkscm.base.authcenter.exception.TenantResourceMoveNodeIdNotFindException;
import com.cloudlinkscm.base.authcenter.exception.TenantResourceMoveOutNodeNotFindException;
import com.cloudlinkscm.base.authcenter.model.PublicResource;
import com.cloudlinkscm.base.authcenter.model.TenantResource;
import com.cloudlinkscm.base.authcenter.model.dto.TenantResourceDistributionDTO;
import com.cloudlinkscm.base.authcenter.model.dto.TenantResourceMoveDTO;
import com.cloudlinkscm.base.authcenter.service.TenantResourceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;

/**
 * Created by sin on 2017/8/7.
 *
 * 租户资源
 */
@Service
public class TenantResourceServiceImpl implements TenantResourceService {

	@Autowired
	private TenantResourceDao tenantResourceDao;
	@Autowired
	private PublicResourceDao publicResourceDao;
	@Autowired
	private AbstractTenantResourceManager tenantResourceManager;
	@Autowired
	private AbstractPublicResourceManager publicResourceManager;


	/**
	 * 租户资源分配：获取租户 资源
	 *
	 * <p>
	 *     返回一个两个 tree
	 *
	 *      1、一个已分配
	 *      2、一个未分配
	 * </p>
	 *
	 * @param tenantId 租户id
	 * @return 返回资源分配 DTO
	 */
	@Override
	public TenantResourceDistributionDTO getResource(String tenantId) {
		List<TenantResource> tenantResourceList = tenantResourceDao.selectByTenantId(tenantId);
		List<PublicResource> publicResourceList = publicResourceManager.listAll();

		if (org.springframework.util.CollectionUtils.isEmpty(tenantResourceList)) {
			tenantResourceList = Collections.EMPTY_LIST;
		}
		if (org.springframework.util.CollectionUtils.isEmpty(publicResourceList)) {
			publicResourceList = Collections.EMPTY_LIST;
		}

		return tenantResourceManager.getResource(publicResourceList, tenantResourceList);
	}


	/**
	 * 租户资源分配：资源移动
	 *
	 * <p>
	 *     资源移动有：移入 和 移出
	 *
	 *     移入：递归 public resource 节点插入 target 节点
	 *
	 *     移出：递归 tenant resource 下的关联节点
	 *
	 *          已分配给 user 的 resource 强制收回
	 * </p>
	 *
	 * @param tenantId 租户编号
	 * @param moveDTO 移动的资源类
	 * @return 受影响条数
	 */
	@Override
	public Integer move(String tenantId, TenantResourceMoveDTO moveDTO) {

		// move in
		if (TenantResourceMoveDTO.MOVE_MODE_IN.equals(moveDTO.getMoveMode())) {

			if (CollectionUtils.isEmpty(moveDTO.getNodeIds())) {
				// empty
			}

			List<PublicResource> basePublicResources = publicResourceDao.selectByIds(moveDTO.getNodeIds());

			if (CollectionUtils.isEmpty(basePublicResources)
					|| basePublicResources.size() != moveDTO.getNodeIds().size()) {

				// public resource not find
				throw new TenantResourceMoveNodeIdNotFindException();
			}

			return tenantResourceManager.moveIn(tenantId, moveDTO.getNodeIds());
		}
		else {

			if (CollectionUtils.isEmpty(moveDTO.getNodeIds())) {
				// empty
			}

			List<TenantResource> baseTenantResources = tenantResourceDao.selectByIds(moveDTO.getNodeIds());

			if (CollectionUtils.isEmpty(baseTenantResources)
					|| baseTenantResources.size() != moveDTO.getNodeIds().size()) {

				// public resource not find
				throw new TenantResourceMoveOutNodeNotFindException();
			}

			return tenantResourceManager.moveOut(moveDTO.getNodeIds());
		}
	}
}
